﻿@{
    Layout = null;
}
<link href="@Url.Content("~/Content/zTreeStyle3.4/zTreeStyle.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery.ztree.all-3.4.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    var _zTreeObj;
    var _setting;
    var _url;
    var _idKey;
    function initSetting(url, idKey, pIdKey, name, async, paras) {
        _url = url;
        _idKey = idKey;
        _setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: idKey,
                    pIdKey: pIdKey
                },
                key: {
                    name: name,
                    title: name
                }
            },
            async: {
                enable: async,
                url: getAsyncUrl,
                otherParam: typeof (paras) == 'undefined' ? null : paras
            },
            callback: {
                onClick: zTreeOnClick,
                onAsyncSuccess: _zTreeOnAsyncSuccess,
                onDblClick: _zTreeOnDblClick,
                beforeExpand: _zTreeBoforeExpand
            }
        };

    }

    function getAsyncUrl(treeId, treeNode) {
        if (treeNode == null) {//如果没有选择节点，说明是首次加载，则应该包含用户所在的单位
            if (_url.indexOf("?") > 0)
                return _url + "&pid="+'@ViewBag.pid'+"&withself=true";
            else
                return _url + "?pid="+'@ViewBag.pid'+"&withself=true";
        }
        else {
            if (_url.indexOf("?") > 0)
                return _url + "&pid=" + eval('treeNode.' + _idKey);
            else
                return _url + "?pid=" + eval('treeNode.' + _idKey);
        }
    };

    //加载树
    function loadBaseDicTree(url, idKey, pIdKey, name, async, paras) {
        initSetting(url, idKey, pIdKey, name, async, paras);
        _zTreeObj = $.fn.zTree.init($("#zTreeObj"), _setting);
    }
    function getSeletedTreeNode() {
        _zTreeObj = $.fn.zTree.getZTreeObj("zTreeObj");
        var nodes = _zTreeObj.getSelectedNodes();
        if (nodes.length > 0) {
            return nodes[0];
        }
        return null;
    }

    function _zTreeOnDblClick(event, treeId, treeNode) {
        if (typeof (zTreeOnDblClick) == 'function') {
            zTreeOnDblClick(event, treeId, treeNode);
        }
    };
    function _zTreeOnAsyncSuccess(event, treeId, treeNode) {
        if (typeof (zTreeOnAsyncSuccess) == 'function') {
            zTreeOnAsyncSuccess(event, treeId, treeNode);
        }
    }
    function _zTreeBoforeExpand(treeId, treeNode) {
        if (typeof (zTreeBoforeExpand) == 'function') {
            zTreeBoforeExpand(treeId, treeNode);
        }
    }
    function getZTreeObj() {
        return _zTreeObj;
    }
</script>
<ul id="zTreeObj" class="ztree">
</ul>
